package com.project.picxg.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;

@Configuration
public class RedisTemplateFactory {
    @Value("${com.redis.host}")
    private String redis_host;
    @Value("${com.redis.port}")
    private int redis_port;
    @Value("${com.redis.timeout}")
    private int redis_timeout;
    @Value("${com.redis.password}")
    private String redis_password;

    private static String host = "127.0.0.1";

    private static int port = 6379;

    private static int timeout = 3000;

    private static String password = "";

    private static int maxActive = 100;

    private static int maxIdle = 8;

    private static int minIdle = 0;

    private static int maxWait = -1;

    @PostConstruct
    public void init(){
        host = redis_host;
        port = redis_port;
        timeout = redis_timeout;
        password = redis_password;
    }

    /**
     * 用于创建redis的Bean
     * @return
     */
    public static RedisTemplate<String, Object> getRedisTemplate(Integer databaseIndex) {
        RedisTemplate<String, Object> redisTemplate = getRedisConnectionFactory(databaseIndex); // 建立Redis的连接
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    /**
     * 是负责建立Factory的连接工厂类
     * @param database
     * @return
     */
    public static RedisTemplate<String, Object> getRedisConnectionFactory(int database) {
        JedisPoolConfig poolConfig = new JedisPoolConfig(); // 进行连接池配置
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxWaitMillis(maxWait);

        JedisConnectionFactory jedisFactory = new JedisConnectionFactory();
        jedisFactory.setHostName(host);
        jedisFactory.setPort(port);
        jedisFactory.setPassword(password);
        jedisFactory.setTimeout(timeout);
        jedisFactory.setDatabase(database);
        jedisFactory.setPoolConfig(poolConfig);
        jedisFactory.afterPropertiesSet(); // 初始化连接池配置

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(jedisFactory);
        return redisTemplate;
    }


}
